package com.hhh.petadopt.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhh.petadopt.mapper.AdminStrayMapper;
import com.hhh.petadopt.pojo.PetFound;
import com.hhh.petadopt.pojo.PetFoundVo;
import com.hhh.petadopt.service.AdminStrayService;
import com.hhh.petadopt.utils.MailUtils;
import com.hhh.petadopt.utils.Page;
/**
 * 流浪动物管理
 * @author 杜镇声声
 *
 */
@Service
public class AdminStrayServiceImpl implements AdminStrayService {
	@Autowired
	private AdminStrayMapper adminStrayMapper;

	//分页显示流浪动物列表
	@Override
	public Page<PetFound> selectPetPage(PetFoundVo Vo) {
		//创建page对象
		Page<PetFound> page = new Page<PetFound>();
		//设置当前页显示的条数
		page.setSize(10);
		Vo.setSize(10);
		if(Vo != null) {
			//封装当前页
			if(null != Vo.getPage()) {
				page.setPage(Vo.getPage());
				//设置起始页
				Vo.setStartRow((Vo.getPage()-1)*Vo.getSize());
			}
			if(null != Vo.getSkind() && !"".equals(Vo.getSkind().trim())) {
				Vo.setSkind(Vo.getSkind().trim());
			}
			if(null != Vo.getSsex() && !"".equals(Vo.getSsex())) {
				Vo.setSsex(Vo.getSsex().trim());
			}
			if(null != Vo.getSdate() && !"".equals(Vo.getSdate().trim())) {
				Vo.setSdate(Vo.getSdate());
			}
			if(null != Vo.getSaccept() && !"".equals(Vo.getSaccept().trim())) {
				Vo.setSaccept(Vo.getSaccept());
			}
			//查询符合条件的总条数
			page.setTotal(adminStrayMapper.selectCount(Vo));
			//查询符合条件的当前页显示的内容
			
			List<PetFound> selectPetList = adminStrayMapper.selectPetList(Vo);
			//将日期转换为yyyy-mm-dd
			List<PetFound> nList = new ArrayList<PetFound>();
			for (PetFound petFound : selectPetList) {
				petFound.setCreated(petFound.getCreated().substring(0, 10));
				nList.add(petFound);
			}
			page.setRows(nList);
			
			
		}
		return page;
	}

	//页面信息回显
	@Override
	public PetFound selectStrayById(String id) {
		//执行查询
		PetFound strayInfo = adminStrayMapper.selectStrayById(id);
		//将日期转换为yyyy-mm-dd格式
		strayInfo.setFtime(strayInfo.getFtime().substring(0, 10));
		return strayInfo;
	}

	//确认受理
	@Override
	public void confirmAceptP(PetFound p) {
		//执行
		adminStrayMapper.confirmAcceptFound(p);
		//给发现人发受理通知邮件
		
		String str = p.getFman() +"先生/女士!您好!您在发现的流浪动物,编号:"+p.getId()+"已被我们受理,我们会对您的这次上报作进一步的确认,同时也非常感谢您的反馈,如有疑问,请电话咨询:123456!";
		try {
			MailUtils.sendMail(p.getFemail(), str);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	//单个删除
	@Override
	public void delStrayById(String id) {
		adminStrayMapper.delStrayById(id);
		
	}

	//删除多个
	@Override
	public void deleteStrayByIds(Integer[] ids) {
		//执行删除操作
		adminStrayMapper.delStrayByIds(ids);
		
	}

}
